<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 账号设置 -->
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form ref="form" :model="formData" :rules="formRules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="formData.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input v-model="formData.password2" style="width:300px" show-password type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <!-- 个人详情 -->
          <el-tab-pane label="个人详情">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <UserInfo @update="initData" />
          </el-tab-pane>
          <!-- 岗位信息 -->
          <el-tab-pane label="岗位信息">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人岗位信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <JobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div></template>

<script>
import { editEmployeesApi, getEmployeesInfoApi } from '@/api/employees'
import JobInfo from '@/views/employees/components/jobInfo.vue'
import UserInfo from '@/views/employees/components/userInfo.vue'
export default {
  components: { JobInfo, UserInfo },
  data() {
    return {
      userId: this.$route.params.id,
      formData: {},
      formRules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password2: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      const res = await getEmployeesInfoApi(this.$route.params.id)
      this.formData = res
    },
    async saveUser() {
      await editEmployeesApi({
        ...this.formData,
        password: this.formData.password2 // 新输入的密码，覆盖了原来的密码
      })
      this.$message.success('更新成功')
    }
  }
}
</script>

<style>

</style>
